package com.hn658.base.message.backend.action.account.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.ext.multipart.Multipart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.hn658.base.message.account.entity.AccountUserInfoEO;
import com.hn658.base.message.account.service.IAccountUserManagerService;
import com.hn658.base.message.backend.action.account.IAccountManagerAction;
import com.hn658.base.message.backend.action.account.vo.AccountManagerRespose;
import com.hn658.framework.common.AbstractBaseService;
import com.hn658.framework.common.IServiceResponse;
import com.hn658.framework.common.ResponseStatus;
import com.hn658.framework.dataaccess.pagination.GenericPage;
import com.hn658.framework.shared.exception.BusinessException;

@Controller("AccountManagerAction")
@Path("account")
@Produces("application/json; charset=UTF-8")
@Consumes("application/json; charset=UTF-8")
public class AccountManagerAction extends AbstractBaseService implements IAccountManagerAction{
	
	@Autowired
	private IAccountUserManagerService userManagerService;
	
	@Path("findList")
	@POST
	@Consumes("application/x-www-form-urlencoded; charset=UTF-8")
	@Produces("application/json; charset=UTF-8")
	public IServiceResponse findUserList(
			@FormParam("loginAccount") String loginAccount,
			@FormParam("nickName") String nickName,
			@FormParam("realName") String realName,
			@FormParam(value="limit") int limit, 
			@FormParam(value="start") int start){
		AccountManagerRespose response=new AccountManagerRespose();
		try {
			List<AccountUserInfoEO> findUserList = userManagerService.findUserList();
			response.setUserInfoEOs(findUserList);
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
			if(e.getErrorMsg()==e.getErrorCode()){
				response.setResponseStatus(new ResponseStatus(e.getErrorMsg(),e.getErrorCode()));
			}
		}
		return response;
	}
	@Path("findByAccountUser")
	@POST
	@Consumes("application/x-www-form-urlencoded; charset=UTF-8")
	@Produces("application/json; charset=UTF-8")
	public IServiceResponse findByAccountUser(@FormParam("loginAccount") String loginAccount,
			@FormParam("isDeleted") String isDeleted,
			@FormParam("nickName") String nickName,
			@FormParam("realName") String realName,
			@FormParam(value="limit") int limit, 
			@FormParam(value="start") int start){
		AccountManagerRespose respose=new AccountManagerRespose();
		Map<String, Object> map=new HashMap<String, Object>();
		try {
			if(StringUtils.isNotBlank(loginAccount)){
				map.put("loginAccount", loginAccount.trim());
			}
			if(StringUtils.isNotBlank(realName)){
				map.put("realName", realName.trim());
			}
			if(StringUtils.isNotBlank(nickName)){
				map.put("nickName", nickName.trim());
			}
			if(StringUtils.isNotBlank(isDeleted)){
				map.put("isDeleted", Boolean.parseBoolean(isDeleted));
			}
			GenericPage<AccountUserInfoEO> findByAccountUser = userManagerService.findByAccountUser(map, limit, start, null, true);
			respose.setUserInfoEOs(findByAccountUser.getData());
			respose.setTotalCount(findByAccountUser.getTotalCount());
			this.success(respose);
		} catch (BusinessException e) {
			this.error(respose, e);
		}
		
		return respose;
		
	}
	
	
	@Path("addUser")
	@Consumes("multipart/form-data; charset=UTF-8")
	@POST
	public IServiceResponse addUser(@Multipart(value = "loginAccount", required = true) String loginAccount,
			@Multipart(value = "passWord", required = true) String passWord,
			@Multipart(value = "sex", required = false) String sex,
			@Multipart(value = "realName", required = true) String realName,
			@Multipart(value = "nickName", required = true) String nickName,
			@Multipart(value = "qq", required = false) String qq,
			@Multipart(value = "weiXin", required = false) String weiXin,
			@Multipart(value = "phoneNumber", required = false) String phoneNumber,
			@Multipart(value = "sign", required = false) String sign,
			@Multipart(value = "avatarUrl", required = false) byte[] file,
			@Context HttpServletRequest request){
		AccountManagerRespose response=new AccountManagerRespose();
		AccountUserInfoEO userInfoEO=new AccountUserInfoEO();
		try {
			userInfoEO.setNickName(realName);
			userInfoEO.setCreateTime(new Date());
			userInfoEO.setLastUpdateTime(new Date());
			userInfoEO.setIsDeleted(false);
			userManagerService.addUser(userInfoEO);
			response.setUserInfoEO(userInfoEO);
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
			if(e.getErrorMsg()==e.getErrorCode()){
				response.setResponseStatus(new ResponseStatus(e.getErrorMsg(),e.getErrorCode()));
			}
		}
		return response;
		
	}
	
	@Path("updateUser")
	@Consumes("multipart/form-data; charset=UTF-8")
	@POST
	public IServiceResponse updateUser(@Multipart(value = "id", required = true) String id,
			@Multipart(value = "nickName", required = true) String nickName,
			@Multipart(value = "userId", required = true) String userId,
			@Multipart(value = "appKey", required = true) String appKey,
			@Multipart(value = "avatarUrl", required = false) byte[] file,
			@Context HttpServletRequest request){
		AccountManagerRespose response=new AccountManagerRespose();
		try {
			AccountUserInfoEO userInfoEO=new AccountUserInfoEO();
			userInfoEO.setId(Long.valueOf(id));
			userInfoEO.setNickName(nickName);
			userInfoEO.setUserId(userId);
			userInfoEO.setAppKey(appKey);
//			if(file!=null&&file.length>0){
//				String[] uris = fileManager.saveBackendAvatar(file, userInfoEO.getId().toString());
//				userInfoEO.setAvatarURL(uris[0]);			
//			}
			userInfoEO.setLastUpdateTime(new Date());
			userManagerService.updateAccount(userInfoEO);
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
			if(e.getErrorMsg()==e.getErrorCode()){
				response.setResponseStatus(new ResponseStatus(e.getErrorMsg(),e.getErrorCode()));
			}
		}
		return response;
	
	}
	@Path("disableUser")
	@Consumes("application/x-www-form-urlencoded; charset=UTF-8")
	@POST
	public IServiceResponse disableUser(@FormParam("id") String id){
		AccountManagerRespose respose=new AccountManagerRespose();
		try {
			if(StringUtils.isBlank(id)){
				throw new BusinessException("请传入正确的id");
			}
			AccountUserInfoEO userInfoEO=new AccountUserInfoEO();
			userInfoEO.setId(Long.parseLong(id));
			userManagerService.disableUser(Long.valueOf(userInfoEO.getId()));
			this.success(respose);
		} catch (BusinessException e) {
			this.error(respose, e);
		}
		return respose;
		
	}
	@Path("enableUser")
	@Consumes("application/x-www-form-urlencoded; charset=UTF-8")
	@POST
	public IServiceResponse enableUser(@FormParam("id") String id){
		AccountManagerRespose respose=new AccountManagerRespose();
		try {
			if(StringUtils.isBlank(id)){
				throw new BusinessException("请传入正确的id");
			}
			AccountUserInfoEO userInfoEO=new AccountUserInfoEO();
			userInfoEO.setId(Long.parseLong(id));
			userManagerService.enableUser(Long.valueOf(userInfoEO.getId()));
			this.success(respose);
		} catch (BusinessException e) {
			this.error(respose, e);
		}
		return respose;
	}
}
